--擂台BOSS
--#include "data\config\boss\ArenaBossConfig.lua" once


--[[
注册一些事件
]]
function ResisterArenaBossProc()
	--print("ResisterArenaBossProc...")
	SetNeedReloginFuben( ArenaBossConfig.fubenId, GlobalLuaConfig.reloginFuben.arenaBossIdx, GetElemsForReLoginArenaBoss )
				--	该副本是需要掉线恢复的
	SceneExitDispatcher.register( ArenaBossConfig.sceneId, OnCommonSceneExit )					--注册离开场景、副本后触发的事件
	SceneHandExitDispatcher.register( ArenaBossConfig.sceneId, OnCommonSceneHandExit )			--注册手工离开场景、副本后触发的事件
	--FubenTimeoutDispatcher.register( ArenaBossConfig.fubenId, OnSingleCommonFubenTimeout )	--注册单人场景、副本时间到后触发的事件
	SceneLogoutDispatcher.register( ArenaBossConfig.sceneId, OnCommonFubenLogout )				--注册场景、副本中下线触发的事件
end

--[[
掉线恢复的一些配置
]]
function GetElemsForReLoginArenaBoss( fubenPtr )
	local fubenCfg 				= ArenaBossConfig
	local InitFubenRightPanel 	= InitArenaBossPanel
	local loginLog   			= "arena boss login" 
	local loginLogOk			= "arena boss login ok" 
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end

function OnArenaBossDailyProc( sysarg )
	--print("OnArenaBossDailyProc...")
	local sVar = Actor.getStaticVar( sysarg )
	sVar.arenaBossTodayKill = nil
end

--[[
判断是否在擂台BOSS副本内
]]
function IsInArenaBossFuben( fubenId )
	return ArenaBossConfig.fubenId == fubenId
end

--[[
进入擂台BOSS副本
]]
function EnterArenaBoss( sysarg )
	--[[
	因为 ArenaBossConfig.reloginType 为1，所以已经调用了Fuben.setOwnerId()设置了副本的ownerId
	]]
	if CommonEnterScence( sysarg, ArenaBossConfig, 0, "", ArenaBossEnterCheck, ArenaBossBeforeEnter ) then
		InitArenaBossVisible( sysarg ) 			--将相关NPC设置为显示或者隐藏
		InitArenaBossPanel( sysarg )
	end
end

--[[
进入副本和掉线重登都会触发
]]
function InitArenaBossPanel( sysarg )
	local contStyle 	= 4					--只显示剩余时间
	local buttons 		= {1, 10}			--{ 退出，选择BOSS }
	local activityId 	= enRightPanelActivity_ArenaBoss
	local title			= Lang.ScriptTips.ArenaBossName
	OpenCommonStylePanel( sysarg, 1, activityId, 0, title, nil, contStyle, buttons )
	SendtArenaBossData( sysarg )
end

function ArenaBossEnterCheck( sysarg, bossCfg )
	if not CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_ArenaBoss, 0 ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0021, ttFlyTip)
		return false
	end
	return true
end

function ArenaBossBeforeEnter( sysarg, bossCfg )
	AddActorDailyDoneTimes( sysarg, enDailyOpTimes_ArenaBoss, bossCfg.bossIdx, 1 ) 				--进入次数+1
	return true
end

function GetArenaBossTodayKill( sysarg, bossIdx )
	local sVar = Actor.getStaticVar( sysarg )
	if not sVar.arenaBossTodayKill then
		sVar.arenaBossTodayKill = {}
	end
	return sVar.arenaBossTodayKill[bossIdx] or 0
end


function SetArenaBossTodayKill( sysarg, bossIdx, newFlag )
	local sVar = Actor.getStaticVar( sysarg )
	if not sVar.arenaBossTodayKill then
		sVar.arenaBossTodayKill = {}
	end
	sVar.arenaBossTodayKill[bossIdx] = newFlag
end

--[[
挑战擂台BOSS
--如果changeLevel要求等级满足，免费召唤；否则扣除changeFee
callType:1-元宝召唤，2-道具召唤
]]
function challengeArenaBoss( sysarg, bossIdx, callType )
	--print("challengeArenaBoss, bossIdx="..bossIdx)
	local bossCfg = ArenaBossConfig.boss[bossIdx]
	if bossCfg then
		local fubenPtr 	  = Actor.getFubenPrt(sysarg)
		local fubenHandle = Fuben.getFubenHandle(fubenPtr)
		local sceneHandle = Actor.getSceneHandle(sysarg)
		if HasArenaBossInArena( fubenHandle ) then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.ArenaBoss002, ttFlyTip)
			return
		end

		local todayKill = GetArenaBossTodayKill( sysarg, bossIdx )
		--print("ChallengeArenaBoss, bossIdx="..bossIdx..", todayKill="..todayKill)
		if todayKill > 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.ArenaBoss001, ttFlyTip)
			return
		end

		--[[
		if not Actor.checkActorLevel( sysarg, bossCfg.challengeLevel[2], bossCfg.challengeLevel[1] ) then
			if bossCfg.challengeFee and not CheckConsumeCond( sysarg, bossCfg.challengeFee, 1, false ) then
				return
			end

			--扣除召唤费用
			local logId  = GameLog.clArenaBossCallFee
			local logStr = Lang.ScriptTips.ArenaBossLog01
			if bossCfg.challengeFee and  not DoConsumeCond( sysarg, bossCfg.challengeFee, 1, logId, logStr) then
				return
			end
		end
		]]

		if not Actor.checkActorLevel( sysarg, bossCfg.recomLevel[2], bossCfg.recomLevel[1] ) then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016, ttFlyTip)
			return
		end

		local consume = nil
		if callType == 1 then
			consume = bossCfg.callBossWithYuanBao
		elseif callType == 2 then
			consume = bossCfg.callBossWithItem
		else
			return
		end

		if not consume then
			return
		end

		if not CheckConsumeCond( sysarg, {consume}, 1, false ) then
			return
		end

		--扣除召唤费用
		local logId  = GameLog.clArenaBossCallFee
		local logStr = Lang.ScriptTips.ArenaBossLog01
		if not DoConsumeCond( sysarg, {consume}, 1, logId, logStr) then
			return
		end

		local monPtr = Fuben.createMonster( sceneHandle, bossCfg.boss.monsterId, bossCfg.boss.pos[1], bossCfg.boss.pos[2], bossCfg.boss.livetime)
		if monPtr then
			Fuben.setFubenTime( fubenHandle, ArenaBossConfig.fubenTime ) 			--召唤BOSS后重置副本时间
			--print("ChallengeArenaBoss, fubenHandle="..fubenHandle..", fubenTime="..ArenaBossConfig.fubenTime)
			local npcPtr = Fuben.getFbNpcPtrById( sceneHandle, bossCfg.npcId )
			if npcPtr then
				Actor.setEntityVisibleFlag(npcPtr, false)
			end

			InitArenaBossPanel( sysarg )
		end
	end
end


--[[
判断擂台上是否已经有BOSS
]]
function HasArenaBossInArena( fubenHandle )
	for bossIdx, bossCfg in ipairs( ArenaBossConfig.boss ) do
		local num = System.getMonsterCountById( bossCfg.boss.bossId, bossCfg.boss.sceneId, fubenHandle )
		if num > 0 then
			return true
		end
	end
	return false
end


--[[
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
注意：怪物死亡时，玩家可能不在线，要确保副本进度能够继续进行
]]
function OnArenaBossDie( monster, sysarg, monId, lastHitKiller )
	--print("OnArenaBossDie, ..monId="..monId)
	local bossCfg = GetArenaBossCfgByBossId( monId )
	if not bossCfg then
	    return
	end
	SetArenaBossTodayKill( sysarg, bossCfg.bossIdx, 1 )
	SendtArenaBossData( sysarg )
end


function GetArenaBossCfgByBossId( bossId )
	for bossIdx, bossCfg in ipairs( ArenaBossConfig.boss ) do
		if bossCfg.boss.monsterId == bossId then
			return bossCfg
		end
	end
	return nil
end

--[[
设置全部NPC为可见状态
]]
function SetArenaBossNpcVisible( sceneHandle )
	for bossIdx, bossCfg in ipairs( ArenaBossConfig.boss ) do
		local npcPtr = Fuben.getFbNpcPtrById( sceneHandle, bossCfg.npcId )
		if npcPtr then
			Actor.setEntityVisibleFlag(npcPtr, true)
		end
	end
end

function SendtArenaBossData( sysarg )
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sSendArenaBossData)
	if npack then
		local fubenHandle = Actor.getFubenHandle( sysarg )
		DataPack.writeByte( npack, #ArenaBossConfig.boss )
		for bossIdx, bossCfg in ipairs( ArenaBossConfig.boss ) do
			local todayKill = GetArenaBossTodayKill( sysarg, bossIdx )
			DataPack.writeByte( npack, bossIdx )
			DataPack.writeByte( npack, todayKill )
			--print("SendtArenaBossData, bossIdx="..bossIdx..", todayKill="..todayKill)
		end
		DataPack.flush(npack)
	end
end

function InitArenaBossVisible( sysarg )
	local sceneHandle = Actor.getSceneHandle( sysarg )
	for bossIdx, bossCfg in ipairs( ArenaBossConfig.boss ) do
		local npcPtr = Fuben.getFbNpcPtrById( sceneHandle, bossCfg.npcId )
		if npcPtr then
			local todayKill = GetArenaBossTodayKill( sysarg, bossIdx )
			if todayKill > 0 then
				Actor.setEntityVisibleFlag(npcPtr, false) 			--已击杀，隐藏NPC
			else
				Actor.setEntityVisibleFlag(npcPtr, true)			--未击杀，显示NPC	
			end
		end
	end
end


ResisterArenaBossProc()
